<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/18
 * Time: 15:40
 */

namespace addons\seller\controller;


use app\common\Code;
use app\common\model\GoodsCategory;
use app\common\model\GoodsCategoryRelation;
use app\common\Session;

class Goods extends Common
{
    /**
     * 商品列表
     * @return mixed
     */
    public function list_()
    {
        $this->check_login();
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN, Code::msg(Code::NO_LOGIN));
        }
        $param = input();
        $this->assign("param", $param);
        $cond['seller_id'] = $seller->id;
        $goods = new \app\common\model\Goods();
        $list_ = $goods->list_($param,$cond);
        $this->assign("list_", $list_);
        if (input('export')) {
            goto EXPORT;
        }
        return $this->fetch();
        EXPORT:
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=data.xls");
        return $this->fetch("export_goods_list");
    }

    /**
     * 加载新增/修改商品视图页
     * @return mixed
     */
    public function edit_()
    {
        $this->check_login();
        $goods_id = input("goods_id");
        if($goods_id)
        {
            $goods = \app\common\model\Goods::get($goods_id);
            $this->assign("goods_data",$goods);
        }
        $goods_category_model = new GoodsCategory();
        #获取有序排序的全部分类
        $goods_category = $goods_category_model->get_all_category();
        $arr = [];
        foreach($goods_category as $item)
        {
            $a1['id'] = $item['id'];
            $a1['pid'] = $item['pid'];
            $a1['category_name'] = $item['category_name'];
            $arr[$a1['id']]=$a1;
        }
        $goods_category = $goods_category_model->get_all_sort_List($arr);
        $this->assign("goods_category",$goods_category);
        #获取已选中商品类型
        if($goods_id)
        {
            $goods_category_relation = new GoodsCategoryRelation();
            $selected_goods_category = $goods_category_relation->getListGoodsCategoryRelation($goods_id); #选中的分类
            $selected_category = "";
            foreach($selected_goods_category as $item){
                $selected_category .= $item['category_name'].'/';
            }
            $this->assign("selected_goods_category", $selected_category);
        }
        return $this->fetch();
    }

    /**
     * 商品操作
     */
    public function edit()
    {
        $seller = Session::get_local_seller();
        if (empty($seller)) {
            response_json(Code::NO_LOGIN);
        }
        $goods_id = input("goods_id");
        $param = input();
        $result = $this->validate(
            $param,
            [
                'goods_name' => 'require',         #商品名称
                'place_of_origin' => 'require',    #产地
                'image' => 'require',              #商品图片
                'pre_price' => 'require|number|gt:0',  #商品原价
                'price' => 'require|number|gt:0',      #商品现价
                'stock' => 'require|number|egt:0',     #库存
                'weight' => 'require|number|gt:0',     #商品重量
                 # 非必须的 seo:优化, goods_category[]:分类，bar_code:编码,content:内容；
            ]);
        if (true !== $result) {
            response_json(Code::PARAM_ERROR, Code::msg(Code::PARAM_ERROR));
        }
        $goods = new \app\common\model\Goods();
        $goods_category_relation = new GoodsCategoryRelation();
        if (empty($goods_id)) {      #新增商品
            $param['seller_id'] = $seller->id;
            $goods_data = $goods->add_goods($param);
            $goods_category_relation->addListGoodsCategoryRelation($goods_data->id,$param['goods_category']);
        }else {                      #编辑商品
            $goods_data = $goods->edit_goods($goods_id,$param);
            if (!empty($param['goods_category'][0]) && $param['goods_category'][0] != 0) {
                //商品类型管理
                $goods_category_relation->delGoodsCategoryRelation($goods_data->id); #删除旧的商品分类关系数据
                $goods_category_relation->addListGoodsCategoryRelation($goods_data->id, $param['goods_category']);
            }
        }
        response_json(Code::NONE);
    }

    /**
     * 删除商品
     */
    public function del()
    {
        $goods_id = input("goods_id");
        $goods = new \app\common\model\Goods();
        $data = $goods->del_goods_by_id($goods_id);
        if($data == true){
            response_json(Code::NONE);
        }else{
            response_json(Code::FAIL);
        }

    }


}